package com.wang.controller;


import com.wang.request.LockProductRequest;
import com.wang.service.ProductService;
import com.wang.util.JsonData;
import com.wang.vo.ProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 落日孤城
 * @since 2025-05-22
 */
@RestController
@RequestMapping("/api/product/v1")
@Api(tags = "商品模块")
public class ProductController {
    @Autowired
    private ProductService productService;

    @ApiOperation("分页查询商品列表")
    @GetMapping("/page_product")
    public JsonData pageProductList(
            @ApiParam("当前页")
            @RequestParam(value = "page",defaultValue = "1")
            Integer page,

            @ApiParam("每页显示多少条")
            @RequestParam(value = "size",defaultValue = "10")
            Integer size
    ) {
        Map<String,Object> result = productService.pageProductList(page,size);
        return JsonData.buildSuccess(result);
    }

    @ApiOperation("获取商品详情")
    @GetMapping("/detail")
    public JsonData detail(@ApiParam("商品ID")
                               @RequestParam(value = "product_id")
                               Long productId){
        ProductVO productVO = productService.findByProductId(productId);
        return JsonData.buildSuccess(productVO);
    }

    @ApiOperation("锁定商品库存")
    @PostMapping("/lock_product")
    public JsonData lockProduct(
            @ApiParam("锁定商品对象")
            @RequestBody
            LockProductRequest lockProductRequest
    ) {
        JsonData json = productService.lockProductStock(lockProductRequest);
        return json;
    }
}

